<template>
  <el-dialog v-model="dialog" title="修改采购商" width="500px" :close-on-click-modal="false">
    <div style="display: flex; align-items: center; justify-content: center;">
      <el-form ref="elForm" :rules="rules" :model="form" label-width="85px" style="width: 400px;">
        <el-form-item label="销售人员" prop="markerGuid">
          <el-select v-model="form.markerGuid">
            <el-option v-for="item of salers" :key="item.listKey" :value="item.listKey" :label="item.listValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" :rows="6" v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <el-button type="primary" @click="confirmUpdate">保存</el-button>
      <el-button @click="dialog = false">关闭</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
  
  import {
    ref,
    reactive,
    inject,
    getCurrentInstance
  } from 'vue'
  
  const $emit = defineEmits(['confirm']);
  const $message = inject('$message');
  const $post = inject('$post');
  const {
    ctx
  } = getCurrentInstance();
  
  const dialog = ref(false);
  
  // 获取所有销售人员
  const salers = ref([]);
  $post('/v2/tdltry/saler').then(res => {
    salers.value = res.data;
  });
  
  // 表单
  const form = reactive({
    tryGuid: '',
    markerGuid: '',
    remark: ''
  });
  
  const rules = {
    markerGuid: [{
      required: true,
      message: '请选择销售人员',
      trigger: 'change'
    }]
  }
  
  const confirmUpdate = () => {
    ctx.$refs.elForm.validate(res => {
      if (!res) return;
      $post('/tdltry/dismarker', form).then(res => {
        $message.success('分配销售人员成功');
        dialog.value = false;
        form.tryGuid = '';
        form.markerGuid = '';
        form.remark = '';
        $emit('confirm');
      })
    })
  }
  
  defineExpose({
    show: function (tryGuid) {
      form.tryGuid = tryGuid;
      dialog.value = true;
    }
  });
  
  
  
</script>

<style>
</style>
